package iscript.uiFramework.components.core
{
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.geom.Matrix;
	import iscript.uiFramework.components.UIComponent;
	
	//TODO: 单张9宫格图的背景。
	public class Background extends UIComponent implements IBackground
	{
		
		private var _repeat:Boolean;
		private var _matrix:Matrix;
		private var _bmpd:BitmapData;
		
		public function Background(bmpd:BitmapData)
		{
			_preferWidth = 100;
			_preferHeight = 100;
			_bmpd = bmpd;
			_matrix = null;
		}
		
		override protected function initialize():void
		{
			super.initialize();
		}
		
		override public function set width(value:Number):void
		{
			_preferWidth = value;
			_begFillBitmap();
			super.height = value;
		}
		
		override public function set height(value:Number):void
		{
			_preferHeight = value;
			_begFillBitmap();
			super.height = value;
		}
		
		
		public function begFill(bmd:BitmapData):void {
			  _bmpd = bmd;
			  _begFillBitmap();
			}
		
		public function begFillColor(value:uint):void {
			  _bmpd = new BitmapData(1, 1, false, value);
			  _begFillBitmap();
			}	
			
		private function _begFillBitmap():void
		{
			if (IsRender)
			{
				graphics.clear();
				if(_bmpd!=null){
					graphics.beginBitmapFill(_bmpd, _matrix, _repeat);
					graphics.moveTo(0, 0);
					graphics.lineTo(_preferWidth, 0);
					graphics.lineTo(_preferWidth, _preferHeight);
					graphics.lineTo(0, _preferHeight);
					graphics.lineTo(0, 0);
					graphics.endFill();
				}
			}
		}
		
		override public function render(container:DisplayObjectContainer = null):void
		{
			_begFillBitmap();
			super.render(container);
		}
		
		public function get repeat():Boolean
		{
			return _repeat;
		}
		
		public function set repeat(value:Boolean):void
		{
			_repeat = value;
			if (IsRender)
				_begFillBitmap()
		}
		
/*		public function get matrix():Matrix
		{
			return _matrix;
		}
		
		public function set matrix(value:Matrix):void
		{
			_matrix = value;
			if (IsRender)
				_begFillBitmap()
		}*/
	
	}

}